﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            try
            {
                //log.Info(new LogContent("test",Helpers.HttpHelper.GetIPAddress()));
                var users = db.Users.OrderByDescending(u => u.Priority).Take(8).ToList();
                var groups = db.UserGroups.OrderByDescending(u => u.Priority).Take(8).ToList();
                var userHots = db.Users.OrderByDescending(u => u.Ups).Take(16).ToList();
                var groupHots = db.UserGroups.OrderByDescending(ug => ug.Ups).Take(12).ToList();
                var links = db.Links.Where(l => l.IsShow == true).OrderByDescending(l => l.Priority).Take(10).ToList();
                var recommmends = db.UserRecommends.OrderByDescending(ur => ur.Priority).Take(12).ToList();
                var industriesGroup = new List<vIndustryGroup>();
                //牛棚行业
                var industryGroupList = db.Database.SqlQuery<Industry>("select * from Industries where IndustryName in (select top 4 Industry from UserGroups group by Industry)").ToList();
                foreach (Industry item in industryGroupList as List<Industry>)
                {
                    vIndustryGroup vindustry = new vIndustryGroup(item);
                    industriesGroup.Add(vindustry);
                }
                var industriesBusiness = new List<vIndustryBusiness>();
                //商家行业
                var industryBusinessList = db.Database.SqlQuery<Industry>("select * from Industries where IndustryName in (select top 4 Industry from Businesses group by Industry)").ToList();
                foreach (Industry item in industryBusinessList as List<Industry>)
                {
                    vIndustryBusiness vindustry = new vIndustryBusiness(item);
                    industriesBusiness.Add(vindustry);
                }
                var anecdotes = new List<vAnecdote>();
                var anecdoteList = db.Anecdotes.Where(c => c.IsPublish == true).OrderByDescending(c => c.Time).Take(4).ToList();
                foreach (Anecdote item in anecdoteList as List<Anecdote>)
                {
                    vAnecdote vanecdote = new vAnecdote(item);
                    anecdotes.Add(vanecdote);
                }
                ViewBag.UsersNum = db.Users.ToList().Count;
                ViewBag.JobsNum = db.Jobs.ToList().Count;
                ViewBag.IndustriesNum = db.Industries.ToList().Count;
                ViewBag.BusinessNum = db.Businesses.ToList().Count;
                ViewBag.GroupNum = db.UserGroups.ToList().Count;
                ViewBag.Users = users;
                ViewBag.Groups = groups;
                ViewBag.UserHots = userHots;
                ViewBag.Links = links;
                ViewBag.Recommmends = recommmends;
                ViewBag.IndustriesGroup = industriesGroup;
                ViewBag.IndustriesBusiness = industriesBusiness;
                ViewBag.Anecdotes = anecdotes;
                ViewBag.SearchHot = db.SearchHots.OrderByDescending(s => s.Num).Take(5).ToList();
                return View();
            }
            catch(Exception ex)
            {
                log.Error(new LogContent("首页出错", HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=网络繁忙，请稍后重试");
            }
        } 
    }
}
